package com.cdhk.listener;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.cdhk.service.CheckIfItHasBeenPublishedService;
import com.hustcad.plm.pdm.workflow.annotion.CustomListener;
import com.hustcad.plm.pdm.workflow.model.constant.ProcessConstant;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.runtime.Execution;

import java.math.BigInteger;
@Slf4j
@CustomListener(
        category = "成都海科",
        description = "检查当前流程对象是否已发布"
)
public  class CheckIfItHasBeenPublishedListener implements ExecutionListener {
    @Override
    public void notify(DelegateExecution execution) {
        long start = System.currentTimeMillis();
        log.info("===============二开监听，成都海科校验流程对象是否已发布，开始时间：{}============", start);
        String workFlowContainId = execution.getProcessInstanceBusinessKey();
        if (StringUtils.isBlank(workFlowContainId)) {
            workFlowContainId = execution.getVariable(ProcessConstant.WORK_FLOW_CONTAINER_ID).toString();
        }
        CheckIfItHasBeenPublishedService checkIfItHasBeenPublishedService = SpringUtil
                .getBean(CheckIfItHasBeenPublishedService.class);
        boolean b = checkIfItHasBeenPublishedService.checkIfItHasBeenPublished(new BigInteger(workFlowContainId));
        RuntimeService runtimeService = SpringUtil.getBean(RuntimeService.class);
        Execution execution1 = runtimeService.createExecutionQuery().processInstanceBusinessKey(workFlowContainId)
                .singleResult();
        runtimeService.setVariable(execution1.getId(), "LCJY", b);
        log.info("===============二开监听，成都海科校验流程对象是否已发布，耗时：{}============",
                 start - System.currentTimeMillis());
    }
}
